package com.fas.system.controller;

import com.fas.common.annotation.Log;
import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.page.TableDataInfo;
import com.fas.common.enums.BusinessType;
import com.fas.common.enums.OperatorType;
import com.fas.common.utils.SecurityUtils;
import com.fas.common.utils.poi.ExcelUtil;
import com.fas.system.domain.FasEquipmentAlarm;
import com.fas.system.service.IFasEquipmentAlarmService;
import com.fas.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备报警 Controller
 *
 * @date 2022-07-13
 */
@Api(tags = "设备报警")
@RestController
@RequestMapping("/system/equipment/alarm")
public class FasEquipmentAlarmController extends BaseController {

    @Resource
    private ISysDeptService deptService;
    @Resource
    private IFasEquipmentAlarmService fasEquipmentAlarmService;

    /**
     * 查询报警日志列表
     */
    @ApiOperation(value = "查询报警日志列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody FasEquipmentAlarm fasEquipmentAlarm, Integer pageNum, Integer pageSize){
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(fasEquipmentAlarm.getDeptId() == null){
                fasEquipmentAlarm.setDeptId(deptId.intValue());
            }
        }
        startPage();
        return getDataTable(fasEquipmentAlarmService.findAllRecord(fasEquipmentAlarm));
    }

    /**
     * PC端-报警日志-导出
     */
    @ApiOperation(value = "PC端-报警日志-导出")
    @PostMapping("/export")
    @Log(title = "报警日志", businessType = BusinessType.EXPORT)
    public void export (HttpServletResponse response,@RequestBody FasEquipmentAlarm fasEquipmentAlarm){
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(fasEquipmentAlarm.getDeptId() == null){
                fasEquipmentAlarm.setDeptId(deptId.intValue());
            }
        }
        List<FasEquipmentAlarm> list = fasEquipmentAlarmService.findAllRecord(fasEquipmentAlarm);
        ExcelUtil<FasEquipmentAlarm> util = new ExcelUtil<>(FasEquipmentAlarm.class);
        util.exportExcel(response, list, "报警日志");
    }

    /**
     * 给报警设备指定巡检员
     */
    @ApiOperation(value = "给报警设备指定巡检员")
    @Log(title = "巡检路线", businessType = BusinessType.UPDATE)
    @PostMapping("/appointIpqc")
    public AjaxResult appointIpqc(@RequestBody FasEquipmentAlarm fasEquipmentAlarm){
        Long userId = SecurityUtils.getUserId();
        fasEquipmentAlarm.setAlarmAckUser(userId.intValue());
        return toAjax(fasEquipmentAlarmService.appointIpqc(fasEquipmentAlarm));
    }

    /**
     * 查看报警记录详细
     */
    @ApiOperation(value = "查看报警记录详细")
    @GetMapping("/detail")
    public AjaxResult detailInfo(String equipmentNumber){
        return AjaxResult.success(fasEquipmentAlarmService.detailInfo(equipmentNumber));
    }

    /**
     * 小程序巡检员-报警巡检列表
     */
    @ApiOperation(value = "小程序巡检员-报警巡检列表")
    @GetMapping("/inspectionList")
    public TableDataInfo inspectionList(Integer pageNum,Integer pageSize){
        Long deptId = SecurityUtils.getDeptId();
        startPage();
        return getDataTable(fasEquipmentAlarmService.inspectionList(deptId.intValue()));
    }

    /**
     * 小程序巡检员-报警巡检列表-提交
     */
    @ApiOperation(value = "小程序巡检员-报警巡检列表-提交")
    @Log(title = "巡检路线", businessType = BusinessType.INSERT ,operatorType = OperatorType.MOBILE)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody FasEquipmentAlarm fasEquipmentAlarm){
        Long userId = SecurityUtils.getUserId();
        fasEquipmentAlarm.setIpqcUserId(userId.intValue());
        return toAjax(fasEquipmentAlarmService.submit(fasEquipmentAlarm));
    }

}
